const util = require('../../../utils/util.js');
const utils = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth: 375,
    height: 667,
    staffid: 0,
    employeeid: 0,
    cardInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 获取屏幕宽度px
    let that = this;
    var windowWidth = wx.getSystemInfoSync().windowWidth*0.92;
    var height = windowWidth*94/69;
    if (options.staffid) {
      that.setData({
        windowWidth: windowWidth,
        height: height
      })
      that.data.staffid = options.staffid;
      that.data.employeeid = options.employeeid;
    }
    that.getMyCardInfo(options.staffid, options.employeeid)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  //获取个人信息
  getMyCardInfo: function (staffid, employeeid) {
    var that = this;
    wx.request({
      url: app.globalData.host + '/api/staff/getinfo?id=' + staffid,
      success: function(res) {
        if (res.data.Code == 0) {
          var obj = res.data.Data;
          that.data.cardInfo = obj;
          that.isAliStore(obj, employeeid);
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      },
    })
  },
  // 是否为支付宝账户
  isAliStore: function (obj, employeeid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/staff/GetPayStore?uid=' + obj.Uid,
      success: function (res) {
        if (res.data.Code == 0){
          let isAliStore = res.data.Data.isAliStore;
          that.getPayCode(obj, employeeid, isAliStore);
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  // 收款码
  getPayCode: function (obj, employeeid, isAliStore) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/CreatePayQrCode?uid=' + obj.Uid + '&employeeId=' + employeeid,
      success: function(res) {
        if (res.data.Code == 0) {
          var code = res.data.Data;
          wx.downloadFile({
            url: utils.formatAvater(code, "https://static.711688.net.cn/upload/paycode/"),
            success: function (res) {
              let payCode = res.tempFilePath;
              that.onCreatePoster(obj,payCode,'','',isAliStore);
            }
          })
        }else{
          util.showModal( res.data.Message);        
        }
      }
    })
  },
  onCreatePoster: function (obj, payCode, wxIcon, alipayIcon, isAliStore) {
    wx.showLoading({title: '生成中...',mask: true})
    const ctx = wx.createCanvasContext('myCanvas');
    wx.createSelectorQuery().select('#canvas-container').boundingClientRect(function (rect) {
      let width = rect.width;
      let height = rect.height;
      ctx.fillStyle = '#fff';
      ctx.fillRect(0, 0, width, height);
      // 绿框高度：
      let greentop = height * 181 / 235;
      ctx.fillRect(0, 0, width, greentop);
      if (payCode) {
        ctx.drawImage(payCode, 0, 0, width, height);
      }
    }).exec()
    setTimeout(function () {
      ctx.draw();
      wx.hideLoading();
    }, 1000)
  },

  savePayCode: function() {
    var that = this;
    wx.showLoading({title: '正在保存',mask: true})
    setTimeout(function() {
      wx.canvasToTempFilePath({
        canvasId: 'myCanvas',
        success: function(res) {
          wx.hideLoading();
          var tempFilePath = res.tempFilePath;
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
              utils.aiCardActionRecord(19);
              wx.showModal({
                content: '图片已保存到相册，赶紧晒一下吧~',
                showCancel: false,
                confirmText: '好的',
                confirmColor: '#333',
                success: function(res) {
                  if (res.confirm) {}
                },
                fail: function(res) {}
              })
            },
            fail: function(res) {
              util.showToast('请删掉小程序再重新授权允许保存至相册');              
            }
          })
        }
      });
    }, 1000);
  },
  fillRoundRect: function (cxt, x, y, width, height, radius, fillColor) {
    //圆的直径必然要小于矩形的宽高          
    if (2 * radius > width || 2 * radius > height) {
      return false;
    }

    cxt.save();
    cxt.translate(x, y);
    //绘制圆角矩形的各个边  
    this.drawRoundRectPath(cxt, width, height, radius);
    cxt.fillStyle = fillColor || "#000"; //若是给定了值就用给定的值否则给予默认值  
    cxt.fill();
  },

  drawRoundRectPath: function (cxt, width, height, radius) {
    cxt.beginPath();
    //从右下角顺时针绘制，弧度从0到1/2PI  
    cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);

    //矩形下边线  
    cxt.lineTo(radius, height);

    //左下角圆弧，弧度从1/2PI到PI  
    cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);

    //矩形左边线  
    cxt.lineTo(0, radius);

    //左上角圆弧，弧度从PI到3/2PI  
    cxt.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);

    //上边线  
    cxt.lineTo(width - radius, 0);

    //右上角圆弧  
    cxt.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);

    //右边线  
    cxt.lineTo(width, height - radius);
    cxt.closePath();
  },

  // getPayCode: function (obj, employeeid, isAliStore) {
  //   let that = this;
  //   wx.request({
  //     url: app.globalData.host + '/api/Staff/CreatePayQrCode?uid=' + obj.Uid + '&employeeId=' + employeeid,
  //     success: function(res) {
  //       if (res.data.Code == 0) {
  //         var code = res.data.Data;
  //         wx.downloadFile({
  //           url: utils.formatAvater(code, "https://static.711688.net.cn/upload/paycode/"),
  //           success: function (res) {
  //             let payCode = res.tempFilePath;
  //             that.getwxIcon(obj, payCode, isAliStore);
  //           }
  //         })
  //       }else{
  //         wx.showModal({
  //           title: '提示',
  //           content: res.data.Message,
  //           showCancel: false
  //         })
  //       }
  //     }
  //   })
  // },

  // 微信支付图标
  // getwxIcon: function (obj, payCode, isAliStore){
  //   let that = this;
  //   wx.downloadFile({
  //     url: 'https://static.711688.net.cn/upload/store/68/blog/thumb500_800/s_1908281513131537398.png',
  //     success: function (res) {
  //      let wxIcon = res.tempFilePath;
  //       that.getalipayIcon(obj, payCode, wxIcon, isAliStore);
  //     }
  //   })
  // },
  
  // //支付宝图标
  // getalipayIcon: function (obj, payCode, wxIcon, isAliStore) {
  //   let that = this;
  //   wx.downloadFile({
  //     url: 'https://static.711688.net.cn/upload/store/68/blog/thumb500_800/s_1910161550490274639.png',
  //     success: function (res) {
  //       let alipayIcon = res.tempFilePath;
  //       that.onCreatePoster(obj, payCode, wxIcon, alipayIcon, isAliStore);
  //     }
  //   })
  // },

  // onCreatePoster: function (obj, payCode, wxIcon, alipayIcon, isAliStore) {
  //   wx.showLoading({title: '生成中...',mask: true})
  //   const ctx = wx.createCanvasContext('myCanvas');
  //   wx.createSelectorQuery().select('#canvas-container').boundingClientRect(function (rect) {
  //     let width = rect.width;
  //     let height = rect.height;
  //     ctx.fillStyle = '#fff';
  //     ctx.fillRect(0, 0, width, height);

  //     // 绿框高度：
  //     let greentop = height * 181 / 235;
  //     ctx.fillStyle = 'rgba(51, 191, 38, 1)';
  //     ctx.fillRect(0, 0, width, greentop);
      
  //     //图片宽度
  //     let imgwidth = width*188/345;
  //     // 高度
  //     let imgheight = imgwidth*215/188;
  //     // 图片左边距
  //     let imgleft = (width-imgwidth)/2;
  //     let imgtop = imgleft*95/79+10;

  //     // 商户名称
  //     ctx.setTextAlign('center')
  //     ctx.font = '24px sans-serif';
  //     ctx.fillStyle = 'rgba(255, 255, 255, 1)';
  //     ctx.fillText(obj.Company, width / 2, 50);

  //     //支持信用卡付款
  //     ctx.setTextAlign('center')
  //     ctx.font = '16px sans-serif';
  //     ctx.fillStyle = 'rgba(255, 255, 255, 1)';
  //     ctx.fillText('● 支持信用卡付款', width / 2, 76);

  //     ctx.fillStyle = 'rgba(255, 255, 255, 1)';
  //     ctx.fillRect(imgleft, imgtop, imgwidth, imgheight);
  //     if (payCode) {
  //       ctx.drawImage(payCode, imgleft + 8, imgtop + 8, imgwidth - 16, imgwidth - 16);
  //     }
  //     ctx.setTextAlign('center');
  //     ctx.font = '21px sans-serif';
  //     ctx.fillStyle = 'rgba(34, 32, 30, 1)';
  //     ctx.fillText('扫码付款', width/2, imgtop + imgwidth + 16);

  //     let appNametop = greentop - (greentop - imgheight - imgtop - 6)/2
  //     ctx.setTextAlign('center');
  //     ctx.fillStyle = 'rgba(255, 255, 255, 1)';
  //     ctx.font = '12px sans-serif';
  //     ctx.fillText('企动 · 玖玖客', width/2, appNametop);

  //     if (!isAliStore){
  //       // 微信支付图标
  //       if (wxIcon) {
  //         ctx.drawImage(wxIcon, imgleft, greentop+33, 48, 42);
  //       }
  //       ctx.fillStyle = 'rgba(72, 72, 72, 1)';
  //       ctx.font = '28px sans-serif';
  //       ctx.fillText('微信支付', imgleft + 120, greentop + 62);
  //     }else{
  //       // 微信支付图标
  //       if (wxIcon) {
  //         ctx.drawImage(wxIcon, imgleft - (width * 60 / 345), greentop + (width * 26 / 345), width * 50 / 345, width*45/345);
  //       }
  //       ctx.fillStyle = 'rgba(72, 72, 72, 1)';
  //       ctx.font = '24px sans-serif';
  //       ctx.fillText('微信支付', imgleft + (width * 48 / 345), greentop + (width * 57 / 345));

  //       // 支付宝图标
  //       if (alipayIcon) {
  //         ctx.drawImage(alipayIcon, imgleft + (width * 121 / 345), greentop + (width * 27 / 345), width * 40 / 345, width * 40 / 345);
  //       }
  //       ctx.fillStyle = 'rgba(72, 72, 72, 1)';
  //       ctx.font = '24px sans-serif';
  //       ctx.fillText('支付宝', imgleft + (width * 205 / 345), greentop + (width * 57 / 345));
  //       ctx.fillStyle = 'rgba(72, 72, 72, 1)';
  //     }
  //   }).exec()
  //   setTimeout(function () {
  //     ctx.draw();
  //     wx.hideLoading();
  //   }, 1000)
  // },
  //点击保存到相册
  

})